package servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.ResultSet;
import java.sql.SQLException;


import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.db.conn;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;



public class mreads_List extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public mreads_List() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	//根据条件返回阅读记录信息
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html;");
		response.setHeader("Access-Control-Allow-Origin", "*");
		response.setHeader("Access-Control-Allow-Methods", "GET,POST");
		PrintWriter out = response.getWriter();
		JSONArray jsonarray = new JSONArray();
		JSONObject jsonobj = new JSONObject();


		conn cn = new conn();

		String f=request.getParameter("f");

		ResultSet rs=null;

		if(f.equals("1"))
		{
			String mid=request.getParameter("mid");
			rs=cn.query("select * from  mreads a " +
					" left join books b on a.bid = b.bid" +
					" left join bmore b2 on a.mid = b2.mid" +
					" where  a.mid2="+mid+" order by id desc ");
		}
		else if(f.equals("2"))
		{
			rs=cn.query("select * from  mreads order by id desc ");
		}

		while (true)
		{
			try {
				if (!rs.next()) break;
				jsonobj.put("id", rs.getString("id"));
				jsonobj.put("mid", rs.getString("mid"));
				jsonobj.put("bid", rs.getString("bid"));
				jsonobj.put("mid2", rs.getString("mid2"));

				jsonobj.put("bname", rs.getString("bname"));
				jsonobj.put("bpic", rs.getString("bpic"));
				jsonobj.put("mtitle", rs.getString("mtitle"));

				jsonarray.add(jsonobj);

			} catch (SQLException e) {
				e.printStackTrace();
			}

		}


		out.println(jsonarray);	
		out.flush();
		out.close();
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}

